<?php

namespace App\Backend\Http\Controllers;

use App\Cat;
use App\Data;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Intervention\Image\Facades\Image;
use Intervention\Image\Imagick\Font;


class DataController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index($type, Request $request)
    {
        $data = $request->all();
        !empty($data['title']) ? $where[] = ['title', 'like', '%' . $data['title'] . '%'] : '';
        !empty($data['id']) ? $where[] = ['id', 'like', '%' . $data['id'] . '%'] : '';
        !empty($data['status']) ? $where['status'] = $data['status'] : '';
        if (!empty($request->name)) {
            $where['user_id'] = User::where('name', $request->name)->value('id');
        }
        if (!empty($request->mobile)) {
            $where['user_id'] = User::where('mobile', $request->mobile)->value('id');
        }
        $where['type'] = $type;
        $where['is_del'] = 0;
        $data['models'] = Data::where($where)->orderBy('id', 'desc')->paginate(20);
        $data['type'] = $type;
        $data['redirecturl'] = $this->redirecturl($type);
        return view('backend::model.model', $data);
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create($id, $type)
    {
        $data['model'] = Data::find($id);
        $data['type'] = $type;
        if ($type == 1 || $type == 2) {
            $data['style'] = Cat::where('type', 8)->get();
        }
        $data['cats'] = Cat::where('type', $type)->get();
        $data['cats'] = collect($data['cats'])->groupBy('father_id');
        switch ($type) {
            case 1:
                return view('backend::model.3dmodelcreate', $data);
                break;
            case 2:
                return view('backend::model.sumodelcreate', $data);
                break;
            case 3:
                return view('backend::model.condrawcreate', $data);
                break;
            case 4:
                return view('backend::model.plancreate', $data);
                break;
            case 5:
                return view('backend::model.templatecreate', $data);
                break;
        }
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
                'type' => 'required',
                'user_id' => 'required',
                'title' => 'required',
                'file' => 'required',
                'filesize' => 'required',
                'image' => 'required',
                'appversion' => 'sometimes|required',
                'renderertype' => 'sometimes|required',
                'cat_id1' => 'required',
                'cat_id2' => 'sometimes|required',
                'style' => 'sometimes|required',
                'coins' => 'required',
                'is_light' => 'sometimes|required',
                'is_vip' => 'required',
                'status' => 'required',
                'note' => ''
            ]
        );
        $images_thumb = '';
        for ($i = 1; $i <= 5; $i++) {
            if (request('image' . $i)) {
                $images_thumb .= request('image' . $i) . ',';
            }
        }
        $images = '';
        for ($i = 1; $i <= 5; $i++) {
            if (request('images' . $i)) {
                $images .= request('images' . $i) . ',';
            }
        }
        $data['images_thumb'] = rtrim($images_thumb, ",");
        $data['images'] = rtrim($images, ",");
        $data['published_at'] = Carbon::now();
        Data::where('id', request('id'))->update($data);
        return redirect('backend/prompt')->with(['message' => '修改成功！', 'status' => true]);
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function showimgs($id)
    {
        $datas = Data::where('id', $id)->value('images');
        $imgs = [
            "title" => "模型原图",
            "id" => 123,
            "start" => 0,
        ];
        foreach ($datas as $k => $im) {
            $imgs['data'][$k]['src'] = env('APP_FILE') . '/' . $im;
            $imgs['data'][$k]['pid'] = $k;
            $imgs['data'][$k]['alt'] = '模型原图';
            $imgs['data'][$k]['thumb'] = '';
        }
        return $imgs;
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function allow()
    {
        Data::where('id', '>', 0)->update(['status' => 1]);
        return redirect('/backend/model/1');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function down($id)
    {
        $data = Data::select('id', 'title', 'file')->find($id);
        $file = public_path('/storage/') . $data->file;
        $filename = $data->title . '.rar';
        return response()->download($file, $filename);
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($type, $id)
    {
        Data::where('id', $id)->update(['is_del' => 1]);
        return redirect('/backend/model/' . $type);
    }

//    单张缩略图绘制
    public function makeonethumb(Request $request)
    {
        $res = $request->all();
        $img_url = public_path('/admin/images/watermark.png');
        $watermark = Image::make($img_url);
        $watermark->text('ID:' . $res['id'], 0, 65, function (Font $font) {
            $font->file(public_path('/admin/images/font.otf'));
            $font->size(28);
            $font->valign('bottom');
            $font->color('#EEE');
        });
        $watermark->opacity(60);
        $img = public_path('storage/' . $res['path']);
        $img = Image::make($img)->widen(1200)->insert($watermark, 'bottom-right', 50, 30);
        $save_path = 'image/' . @date("Ymd", time()) . '/' . md5(rand(100000000, 999999999)) . '_thumb' . '.' . $img->extension;
        $real_save_path = public_path('storage/') . $save_path;
        $img->save($real_save_path);
        return $save_path;
    }

    public function redirecturl($type)
    {
        switch ($type){
            case '1' :
                return '/modeInfo';
            case '2' :
                return '/ModeInfoSu';
            case '3' :
                return '/modeInfoConstr';
            case '4' :
                return '/modeInfoConcept';
        }
    }
}
